package cn.imnu.legalaid.controller;

import cn.imnu.legalaid.dto.Consultation.*;
import cn.imnu.legalaid.entity.Consultations;
import cn.imnu.legalaid.service.ConsultationsService;
import cn.imnu.legalaid.util.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/consultations")
public class ConsultationController {

    @Autowired
    private ConsultationsService consultationService;

    // 创建咨询
    @PostMapping
    public Result<Map<String, Object>> createConsultation(@RequestBody ConsultationDTO consultationDTO) {
        try {
            Integer consultationId = consultationService.createConsultation(consultationDTO);
            Map<String, Object> data = new HashMap<>();
            data.put("id", consultationId);
            return Result.success(data, "咨询创建成功");
        } catch (Exception e) {
            log.error("创建咨询失败", e);
            return Result.error("创建咨询失败: " + e.getMessage());
        }
    }

    // 获取咨询列表（分页+筛选）
    @GetMapping
    public Result<Map<String, Object>> getConsultations(ConsultationQueryDTO queryDTO) {
        try {
            // 设置分页参数
            Page<ConsultationDTO> page = new Page<>(queryDTO.getPage(), queryDTO.getSize());
            Page<ConsultationDTO> result = consultationService.getConsultations(page, queryDTO);

            Map<String, Object> data = new HashMap<>();
            data.put("records", result.getRecords());
            data.put("total", result.getTotal());
            data.put("current", result.getCurrent());
            data.put("size", result.getSize());

            return Result.success(data, "获取咨询列表成功");
        } catch (Exception e) {
            log.error("获取咨询列表失败", e);
            return Result.error("获取咨询列表失败");
        }
    }

    // 获取咨询详情
    @GetMapping("/{id}")
    public Result<ConsultationDTO> getConsultationDetail(@PathVariable Integer id) {
        try {
            ConsultationDTO consultation = consultationService.getConsultationDetail(id);
            if (consultation == null) {
                return Result.error("咨询不存在");
            }
            return Result.success(consultation, "获取咨询详情成功");
        } catch (Exception e) {
            log.error("获取咨询详情失败", e);
            return Result.error("获取咨询详情失败");
        }
    }

    // 律师接取咨询
    @PutMapping("/{id}/accept")
    public Result<ConsultationDTO> acceptConsultation(@PathVariable Integer id, @RequestBody ConsultationAcceptDTO acceptDTO) {
        try {
            ConsultationDTO consultation = consultationService.acceptConsultation(id, acceptDTO.getLawyerId());
            return Result.success(consultation, "接单成功");
        } catch (Exception e) {
            log.error("接单失败", e);
            return Result.error("接单失败: " + e.getMessage());
        }
    }

    // 更新咨询状态
    @PutMapping("/{id}/status")
    public Result<Void> updateConsultationStatus(@PathVariable Integer id, @RequestBody Map<String, String> request) {
        try {
            String status = request.get("status");
            consultationService.updateConsultationStatus(id, status);
            return Result.success(null, "咨询状态更新成功");
        } catch (Exception e) {
            log.error("更新咨询状态失败", e);
            return Result.error("更新咨询状态失败");
        }
    }

    // 获取消息列表
    @GetMapping("/{consultationId}/messages")
    public Result<List<MessageDTO>> getMessages(@PathVariable Integer consultationId) {
        try {
            List<MessageDTO> messages = consultationService.getMessages(consultationId);
            return Result.success(messages, "获取消息成功");
        } catch (Exception e) {
            log.error("获取消息失败", e);
            return Result.error("获取消息失败");
        }
    }

    // 发送消息
    @PostMapping("/{consultationId}/messages")
    public Result<Map<String, Object>> sendMessage(@PathVariable Integer consultationId, @RequestBody MessageDTO messageDTO) {
        try {
            messageDTO.setConsultationId(consultationId);
            Integer messageId = consultationService.sendMessage(messageDTO);

            Map<String, Object> data = new HashMap<>();
            data.put("id", messageId);
            return Result.success(data, "消息发送成功");
        } catch (Exception e) {
            log.error("发送消息失败", e);
            return Result.error("发送消息失败");
        }
    }

    // 标记消息为已读
    @PutMapping("/{consultationId}/messages/read")
    public Result<Void> markAsRead(@PathVariable Integer consultationId, @RequestBody Map<String, List<Integer>> request) {
        try {
            List<Integer> messageIds = request.get("messageIds");
            consultationService.markMessagesAsRead(consultationId, messageIds);
            return Result.success(null, "消息已标记为已读");
        } catch (Exception e) {
            log.error("标记消息已读失败", e);
            return Result.error("标记消息已读失败");
        }
    }

    // 提交评价
    @PostMapping("/{consultationId}/review")
    public Result<Void> submitReview(@PathVariable Integer consultationId, @RequestBody ReviewDTO reviewDTO) {
        try {
            consultationService.submitReview(consultationId, reviewDTO);
            return Result.success(null, "评价提交成功");
        } catch (Exception e) {
            log.error("提交评价失败", e);
            return Result.error("提交评价失败");
        }
    }
}